// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================

package tc.crase.cmds;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;

import tc.common.registry.RegistrySys;
import tc.common.symbology.Style;
import tc.common.utility.Dynbuff;
import tc.common.utility.DynbuffLine;
import tc.common.utility.DynbuffText;
import tc.common.utility.GrView;
import tc.common.utility.TcDisplayXY;

import tc.crase.db.XattrGIT;
import tc.crase.db.XattrGRP;
import tc.crase.db.XattrGXY;
import tc.crase.db.Xdb;
import tc.crase.meta.Metadata;
import tc.crase.sys.XdbEx;
import tc.crase.sys.XdbRoot;
import tc.crase.sys.XobjectEx;
import tc.crase.view.GrDocGraph;
import tc.crase.view.ViewGraph;
import tc.crase.view.ViewRoot;
import tc.crase.meta.Type;

public class CmdCreate extends CmdEx implements ActionListener {

	XobjectTypeModel typeModel;
	JComboBox typeCombo;
	JTextField nameField;
	
	XobjectEx xobj = null;

	public CmdCreate() {
	}

	public void initializeDialog() {
		super.initializeDialog();
		setLabel("Create Object");
		setHint("Click on button to create object. Just cteated object can be added in a graph.");

		Metadata meta = XdbRoot.xdbsys.current.metadata;
		Type[] types = meta.types.values().toArray(new Type[0]);

		for (int i = 0; i < types.length; i++) {
			if (i != 0) dialog.add(Box.createRigidArea(new Dimension(0,3)));
			
			Type type = types[i];
			JButton button = new JButton(type.id);
			button.addActionListener(this);
			button.setPreferredSize(new Dimension(SIDEBAR_WIDTH, 24));
			button.setMaximumSize(new Dimension(SIDEBAR_WIDTH, 24));
			button.setActionCommand(type.id);

			dialog.add(button);
		}

		finalizeDialog();
	}

	public void enterPoint(Point pt, GrView view) {
		if (xobj == null) return;
		if (view instanceof ViewGraph == false) return;
		
		ViewGraph viewGraph = (ViewGraph)view;
		GrDocGraph grDocGraph = viewGraph.getGrDocGraph();
		if (grDocGraph == null) return;
		
		XattrGRP attrGraph = grDocGraph.getDocGraph();
		for ( XattrGIT item : attrGraph) {
			if (item instanceof XattrGXY == false) continue;
			if ( item.getParent() == xobj ) return;
		}
		
		TcDisplayXY PT = new TcDisplayXY(new Point2D.Double(pt.x, pt.y),
				view.transf, TcDisplayXY.CoordSys.DEVICE);
		PT.transform(TcDisplayXY.CoordSys.WORLD);
				
		RegistrySys.registry.lock(); {
			attrGraph.createGXY(xobj, PT.xy);
			Selection.clean();
		} RegistrySys.registry.unlock();
		
		//xobj = null;
		
		Dynbuff.dynamicBufferClean();
	}

	@Override
	public void enterReset(Point pt, GrView view) {
	}
	
	public Style getStyle(XobjectEx object) { //TODO - common method
		Style style = null;
		
		Xdb xdb = object.getDb();
		if (xdb instanceof XdbEx) {
			XdbEx xdbex = (XdbEx)xdb;
			style = (Style)xdbex.metadata.legend.styles.get(object.getType());
		}
		
		return style;
	}
	
	@Override
	public void enterMove(Point pt, GrView view) {
		Dynbuff.dynamicBufferClean();
		
		if (xobj == null) { Dynbuff.addCross(pt, (JPanel)view); return; }
		
		if (view instanceof ViewGraph == false) return;
		
		ViewGraph viewGraph = (ViewGraph)view;
		GrDocGraph grDocGraph = viewGraph.getGrDocGraph();
		if (grDocGraph == null) { Dynbuff.addCross(pt, (JPanel)view); return; }
		
		XattrGRP attrGraph = grDocGraph.getDocGraph();
		for ( XattrGIT item : attrGraph) {
			if (item instanceof XattrGXY == false) continue;
			if ( item.getParent() != xobj ) continue;
			Dynbuff.addCross(pt, (JPanel)view);
			return;
		}
		
		Dynbuff.dynamicBufferClean();
		
		DynbuffText text = new DynbuffText(pt, xobj.getName(), getStyle(xobj));
		Dynbuff.dynamicBufferAdd(text, Dynbuff.Buffer.CMD, (JPanel)view);

	}

	public void actionPerformed(ActionEvent e) {
		XdbEx xdb = XdbRoot.xdbsys.current;
		Metadata meta = XdbRoot.xdbsys.current.metadata;
		Type type = meta.types.get(e.getActionCommand());
		
		xobj = xdb.createObject(-1);
		
		if (type != null) {
			type.initialize(xobj);
		}
		
		ViewRoot.showObject(xobj);
	}
}
